<template>
    <div class="header">
        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="collapseChage">
            <i v-if="!collapse"><img style="margin-top: 5px;width: 80%;height: 45%" src="../assets/img/white1.png"></i>
            <i v-else><img style="margin-top: 5px;width: 80%;height: 45%" src="../assets/img/white1.png"></i>
        </div>
        <div class="logo">供应商档案管理系统</div>
        <div class="header-right">
            <div class="header-user-con">
                <!-- 用户名下拉菜单 -->
                <el-dropdown class="user-name" trigger="hover" :hide-on-click="false" @command="handleCommand">
                        <span class="el-dropdown-link">
                            <i class="el-icon-user" style="font-size: 28px; font-weight: bold; "><span style="font-size: 15px;margin-left: 8px">{{username}}</span></i>
                            <i class="el-icon-caret-bottom"></i>
                        </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item divided command="handleEdit" icon="el-icon-key">重置密码</el-dropdown-item>
                        <el-dropdown-item divided command="loginout" icon="el-icon-switch-button">退出登录
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
        <!--重置密码弹出框-->
        <el-dialog title="重置密码" :visible.sync="editVisible" width="20%">
            <el-form ref="editForm" :rules="editFormRules" :model="editForm" label-width="100px">
                <el-form-item label="账号:" prop="userAcct">
                    <el-input disabled v-model="editForm.userAcct"></el-input>
                </el-form-item>
                <el-form-item label="原密码:" prop="userPwd">
                    <el-input type="password" v-model="editForm.userPwd"></el-input>
                </el-form-item>
                <el-form-item label="修改密码:" prop="userUPwd">
                    <el-input type="password" v-model="editForm.userUPwd"></el-input>
                </el-form-item>
                <el-form-item label="确认修改密码:" prop="userQUPwd">
                    <el-input type="password" v-model="editForm.userQUPwd"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                    <el-button @click="closeEdit">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
        </el-dialog>
    </div>
</template>

<script>
    import bus from '../components/common/bus';
    import {doSignout} from "../api/login";
    import {updatePwd} from "../api/user";

    export default {
        data: function () {
            return {
                collapse: false,
                editVisible: false,
                editForm: {},
                count: 3,
                icons: ["el-icon-odometer", "el-icon-basketball", 'el-icon-reading'],
                list: [{'isLink': true, url: "/assess"}, {'isLink': true, url: "https://weibo.com/"}, {
                    'isLink': true,
                    url: "https://weibo.com/mygroups?gid=3862264912296770"
                }],
                backgroundColor: '#6690ae',
                color: '#ffffff',
                position: 'bottom-left',
                isOpenNewTab: true,
                editFormRules: {
                    userPwd: [
                        {required: true, message: '请输入用户原密码', trigger: 'blur'},
                        {pattern: /^[^\u4e00-\u9fa5]+$/, message: '不允许输入中文', trigger: 'blur'},
                        {pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+$/, message: '不允许输入空格等特殊符号', trigger: 'blur'},
                        {pattern: /[0-9a-zA-Z]{1,6}/, message: '只可以输入数字和字母', trigger: 'blur'},
                        {min: 6, max: 6, message: '密码必须为6位字符', trigger: 'blur'}
                    ],
                    userUPwd: [
                        {required: true, message: '请输入修改后的密码', trigger: 'blur'},
                        {pattern: /^[^\u4e00-\u9fa5]+$/, message: '不允许输入中文', trigger: 'blur'},
                        {pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+$/, message: '不允许输入空格等特殊符号', trigger: 'blur'},
                        {pattern: /[0-9a-zA-Z]{1,6}/, message: '只可以输入数字和字母', trigger: 'blur'},
                        {min: 6, max: 6, message: '修改后的密码必须为6位字符', trigger: 'blur'}
                    ],
                    userQPwd: [
                        {required: true, message: '请输入确认修改后的密码', trigger: 'blur'},
                        {pattern: /^[^\u4e00-\u9fa5]+$/, message: '不允许输入中文', trigger: 'blur'},
                        {pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+$/, message: '不允许输入空格等特殊符号', trigger: 'blur'},
                        {pattern: /[0-9a-zA-Z]{1,6}/, message: '只可以输入数字和字母', trigger: 'blur'},
                        {min: 6, max: 6, message: '确认修改后的密码必须为6位字符', trigger: 'blur'}
                    ],
                },
                name: '',
            };
        },
        computed: {
            username() {
                let username = localStorage.getItem('ms_userAcct');
                return username ? username : this.name;
            }
        },
        methods: {
            // 用户名下拉菜单选择事件
            handleCommand(command) {
                if (command == 'loginout') {//退出登录
                    // 二次确认退出登录
                    this.$confirm('确定要退出登录吗？', '提示', {
                        type: 'warning'
                    })
                        .then(() => {
                            //后台进行退出登录操作
                            doSignout({
                                userAcct: localStorage.getItem('ms_userAcct')
                            }).then(res => {
                                if (res.success == true) {
                                    localStorage.removeItem('Authorization');
                                    localStorage.removeItem('ms_userAcct');
                                    localStorage.removeItem('ms_roleId');
                                    this.$router.push('/login');
                                }
                            });
                        })
                        .catch(() => {
                            this.$message({
                                message: '已取消操作'
                            });
                        });
                }
                if (command == 'handleEdit') {//重置密码
                    this.editForm.userAcct = localStorage.getItem('ms_userAcct');
                    this.editVisible = true;
                }
            },
            closeEdit() {//关闭重置密码弹出框
                this.editVisible = false;
                this.editForm = {};
            },
            saveEdit() {
                // validate 函数 表单验证
                this.$refs['editForm'].validate((valid) => {
                    if (valid) {
                        //判断修改密码与确认修改密码是否一致
                        if (this.editForm.userUPwd != this.editForm.userQUPwd) {//不一致
                            this.$message.error("请确认两次输入的修改密码是否一致？");
                        } else {//一致
                            //调用后台
                            updatePwd({
                                userAcct: this.editForm.userAcct,
                                oldPassword: this.editForm.userPwd,
                                newPassword: this.editForm.userQUPwd
                            }).then(res => {
                                if (res.success) {
                                    if (res.data == 1) {
                                        this.editVisible = false;
                                        this.editForm = {};
                                        this.$message.success('重置成功请重新登录');
                                        localStorage.removeItem('ms_userAcct');
                                        this.$router.push('/login');
                                    } else {
                                        this.$message.error("重置失败");
                                    }
                                } else {
                                    this.$message.error(res.data);
                                }
                            });
                        }
                    }
                })
            },
            // 侧边栏折叠
            collapseChage() {
                this.collapse = !this.collapse;
                bus.$emit('collapse', this.collapse);
            },
        },
    };
</script>

<style scoped>
    .header {
        position: relative;
        box-sizing: border-box;
        width: 100%;
        height: 8%;
        font-size: 28px;
        color: #fff;
        background-color: #14306b;
        /*background: linear-gradient(to top, #303143 0%, #dadcd5 100%);*/
    }

    .collapse-btn {
        float: left;
        background-color: #14306b;
        width:10%;
    }

    .header .logo {
        float: left;
        width: 40%;
        line-height: 50px;
        margin-top: 6px;
    }

    .header-right {
        float: right;
        padding-right: 70px;
    }

    .header-user-con {
        display: flex;
        height: 50px;
        align-items: center;
    }

    .btn-bell .el-icon-bell {
        color: #fff;
    }

    .user-name {
        margin-left: 16px;
    }

    cascader {
        margin-left: 10px;
    }

    .user-avator img {
        display: block;
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }

    .el-dropdown-link {
        color: #fff;
        cursor: pointer;
    }

    .header-avator img {
        display: block;
        width: 8%;
        height: 8%;
        border-radius: 50%;
    }

</style>
